/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package golftourcalculator;

import java.io.IOException;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author Martin
 */
public class HTMLParser {

    public class HTMLParserException extends Exception {

        HTMLParserException(String message) {
            super(message);
        }
    }

    private final ObservableList<TournamentResult> tournamentResultList;
    private final String[] bruttoUrlStrings;
    private final String[] nettoUrlStrings;

    HTMLParser(String[] bruttoUrlStrings, String[] nettoUrlStrings) {

        this.tournamentResultList = FXCollections.observableArrayList();
        this.bruttoUrlStrings = bruttoUrlStrings;
        this.nettoUrlStrings = nettoUrlStrings;
    }

    /**
     *
     * @throws IOException
     * @throws golftourcalculator.HTMLParser.HTMLParserException
     */
    public void parse() throws IOException, HTMLParserException {

        ArrayList<Document> docsBrutto = new ArrayList<>();
        for (String bruttoUrlString : bruttoUrlStrings) {
            docsBrutto.add(Jsoup.connect(bruttoUrlString).timeout(0).get());
        }

        String selectorRecordTable = "table#ctl00_MainPlaceHolder_ResultList1_gvResultList";
        String selectorRows = "tr[class*=Row]";

        Document docNetto;
        for (String nettoUrlString : nettoUrlStrings) {

            docNetto = Jsoup.connect(nettoUrlString).timeout(0).get();

            Element recordTableNetto = docNetto.select(selectorRecordTable).first();
            if (recordTableNetto != null) {

                Elements rows = recordTableNetto.select(selectorRows);
                for (Element row : rows) {

                    if (row.childNodeSize() > 6) {

                        // search netto data
                        Elements line = row.select("td");

                        int rank = Integer.parseInt(line.get(0).text());

                        String playerName = line.get(1).text();

                        String clubAbbreviation = line.get(2).text();

                        String cardId = line.get(3).text();

                        int netto = Integer.parseInt(line.get(6).text());

                        int brutto = 0;

                        // search brutto data
                        for (Document docBrutto : docsBrutto) {

                            Element recordTableBrutto = docBrutto.select(selectorRecordTable).first();
                            if (recordTableBrutto != null) {

                                Elements rowsBrutto = recordTableBrutto.select(selectorRows);
                                for (Element rowBrutto : rowsBrutto) {
                                    Elements lineBrutto = rowBrutto.select("td");

                                    String cardIdBrutto = lineBrutto.get(3).text();

                                    if (cardIdBrutto.equals(cardId)) {
                                        brutto = Integer.parseInt(lineBrutto.get(6).text());
                                    }
                                }
                            } else {
                                throw new HTMLParserException("Tabulka s výsledky brutto nenalezena.");
                            }
                        }

                        // add row to the list
                        TournamentResult tournamentResult = new TournamentResult(rank, playerName, clubAbbreviation, cardId, brutto, netto);
                        this.tournamentResultList.add(tournamentResult);
                    } else {
                        throw new HTMLParserException("Tabulka má špatný formát.");
                    }
                }
            } else {
                throw new HTMLParserException("Tabulka s výsledky netto nenalezena.");
            }
        }
    }

    public ObservableList<TournamentResult> result() {
        return this.tournamentResultList;
    }

}
